package com.sixth.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sixth.dao.BusCustomerDao;
import com.sixth.entity.BusCustomer;
import com.sixth.entity.User;
import com.sixth.service.BusCustomerService;
import com.sixth.util.CommonResult;
import com.sixth.util.TableLayUi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @program: carrent
 * @description:
 * @author: 老范
 * @create: 2022-01-12 17:21
 **/
@Service(value = "busCustomerService")
public class BusCustomerServicelmpl implements BusCustomerService {
    @Autowired
    private BusCustomerDao busCustomerDao;
    @Autowired
    private HttpSession session;
    public TableLayUi findBusCustomer(int page,int limit){
        PageHelper.startPage(page,limit);
        List<BusCustomer> busCustomers=busCustomerDao.findAll();
        PageInfo<BusCustomer>pageInfo=new PageInfo<BusCustomer>(busCustomers);
        return new TableLayUi(0,"",pageInfo.getTotal(),pageInfo.getList());
    }

    @Override
    public CommonResult add(String identity, String custname, String phone, String address, String career, Integer sex, Date createtime) {

        //获取当前时间
        Date date = new Date();
        createtime=date;
        int i = busCustomerDao.add(identity, custname, phone, address, career, sex, createtime);
        if (i > 0) {
           return new CommonResult(1,"操作成功",null);
        }
        return new CommonResult(2,"操作成功",null);
    }

    @Override
    public CommonResult update(BusCustomer busCustomer) {
        System.out.println(busCustomer);
        int i = busCustomerDao.updateByPrimaryKey(busCustomer);
        if (i > 0) {
            return new CommonResult(1,"操作成功",null);
        }
        return new CommonResult(2,"操作成功",null);
    }

//    public CommonResult addOrUpdate(BusCustomer busCustomer){
//        System.out.println(addOrUpdate(busCustomer));
//        int i;
//        if (busCustomer.getIdentity() !=null){
//            i=busCustomerDao.insert(busCustomer);
//        }else {
//            i=busCustomerDao.update(busCustomer);
//        }
//        CommonResult commonResult;
//        if (i > 0) {
//            commonResult = CommonResult.success( null, "操作成功");
//        } else {
//            commonResult = CommonResult.fail("操作失败");
//        }
//
//        return commonResult;
//
//        }



        public CommonResult delete(String identity){
        Integer i=busCustomerDao.deleteByPrimaryKey(identity);
        CommonResult commonResult;
            if (i > 0) {
                commonResult = CommonResult.success(null, "删除成功");
            } else {
                commonResult = CommonResult.fail("删除失败");
            }

            return commonResult;
        }
    //批量删除
    public CommonResult batch(String identity) {
        List<BusCustomer> busCustomers = JSONArray.parseArray(identity, BusCustomer.class);
        BusCustomer u = new BusCustomer();
        for (int i = 0; i < busCustomers.size(); i++) {
            u.setIdentity(busCustomers.get(i).getIdentity());
            String Iid = u.getIdentity();
            int j = busCustomerDao.dels(Iid);
            if (j == 0) {
                return new CommonResult(5000, "操作失败", null);
            }
        }
        return new CommonResult(2000,"操作成功",null);
    }
    public TableLayUi listByCondition(int page,int limit,String identity,String custname,String phone,String address,String career,Integer sex){
        PageHelper.startPage(page,limit);
        List<BusCustomer> busCustomers=busCustomerDao.listByCondition(identity,custname,phone,address,career,sex);
        PageInfo<BusCustomer> pageInfo=new PageInfo<BusCustomer>(busCustomers);
        return new TableLayUi(0,"",pageInfo.getTotal(),pageInfo.getList());
    }
    }

